package com.it.controller;

import com.it.pojo.ClOrder;
import com.it.service.ClOrderService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class SysOrderController {

    @Autowired    //自动注入(由spring创建当前对象并为属性赋值)
    private ClOrderService clOrderService;

    /** 1.查询所有订单信息 */
    @RequestMapping("/sysOrderList")
    public String sysOrderList(Model model){
        //调用service层的findAll方法，查询所有的订单信息
        try{
            List<ClOrder> list = clOrderService.findAll();

            //将所有订单信息的list集合存入model中，带到JSP显示
            model.addAttribute("list", list);
        }catch(RuntimeException re ){
            re.printStackTrace();
        }

        return "sys/order/list";
    }

    /**
     * 2.根据id删除订单信息
     */
    @RequestMapping("/sysOrderDelete")
    public String sysOrderDelete(String id){
        //调用deleteById方法, 根据id删除订单
        clOrderService.deleteById(id);

        return "redirect:/sysOrderList";
    }

    /** 3.添加订单信息 */
    @RequestMapping("/sysOrderAdd")
    public String sysOrderAdd(ClOrder clOrder){
        //System.out.println(clOrder);
        //1.调用ClOrderService层的addClOrder方法, 添加订单信息
        clOrderService.addClOrder(clOrder);

        return "redirect:/sysOrderList";
    }

    /** 4.根据id修改订单信息
     *  4.1.先根据id查询当前订单的信息 */
    @RequestMapping("/sysOrderInfo")
    public String sysOrderInfo(HttpServletRequest request, String id, Model model){
        if(StringUtils.isNullOrEmpty(id)){
            id = request.getParameter("id");
        }
        //1.根据id查询订单信息
        ClOrder clOrder = clOrderService.findById(id);

        //2.将订单信息存入model中(存入request域中)
        model.addAttribute("order", clOrder);
        //3.转向clOrder_update.jsp
        return "sys/order/update";
    }

    /**
     * 5.根据id修改订单信息
     **/
    @RequestMapping("/sysOrderUpdate")
    public String sysOrderUpdate(ClOrder clOrder){
        //System.out.println(clOrder);
        //1.调用clOrderService的updateById方法修改订单信息
        clOrderService.updateById(clOrder);
        //2.转向查询所有订单的方法
        return "redirect:/sysOrderList";
    }

    /**
     * 5.1、出单
     */
    @RequestMapping("/sys/update/starttime")
    public String updateStarttime(String orderId, String deliverName, String deliverMobile){
        ClOrder order = clOrderService.findById(orderId);
        order.setStartTime(new Date());
        order.setStatus(1);
        order.setDeliverName(deliverName);
        order.setDeliverMobile(deliverMobile);
        try{
            clOrderService.updateById(order);
        }catch(RuntimeException re){
            re.printStackTrace();
        }
        return "redirect:/sysOrderList";
    }

    /**
     * 5.2、消单
     */
    @RequestMapping("/sys/update/canceltime")
    public String updateCanceltime(String orderId){
        ClOrder order = clOrderService.findById(orderId);
        order.setCancelTime(new Date());
        order.setStatus(-2);
        try{
            clOrderService.updateById(order);
        }catch(RuntimeException re){
            re.printStackTrace();
        }
        return "redirect:/sysOrderList";
    }

}
